/**
[
    {
        expressCompany: '普通快递',
        expressData: [
          {
            area: [
                {id: 1, province: '江西省'},
                {id: 3, province: '浙江省'}
            ],
            data: {
                expressFree: 1,
                expressFee: 0
            }
          },
          {
            area: [
                {id: 2, province: '广东省'},
                {id: 4, province: '江苏省'}
            ],
            data: {
                expressFree: 0,
                expressFee: 2
            }
          }
        ]
   }
]
 */
export const convertJson = (responseExpressTemplateItemData) => {
  let expressJson = []
  let idx = 0
  responseExpressTemplateItemData.forEach(item => {
    if (idx === 0) {
      expressCompanyFirstData(expressJson, item)
    } else {
      let newExpressCompany = true
      for (let i = 0, len = expressJson.length; i < len; i++) {
        if (expressJson[i].expressCompany === item.expressCompany) {
          // 已经有的快递公司
          sameExpressCompanyData(expressJson, i, item)
          newExpressCompany = false
          break
        }
      }
      if (newExpressCompany) {
        // 新的快递公司
        expressCompanyFirstData(expressJson, item)
      }
    }
    idx++
  })
  return expressJson
}

export const expressCompanyFirstData = (expressJson, item) => {
  expressJson.push(buildExpressData(item))
}

export const sameExpressCompanyData = (expressJson, index, expressTemplateItem) => {
  let added = false
  let expressDataJson = expressJson[index].expressData
  for (let i = 0, len = expressDataJson.length; i < len; i++) {
    let theData = expressDataJson[i]
    if (theData.data.expressFree === expressTemplateItem.expressFree
      && theData.data.expressFee === expressTemplateItem.expressFee
      && theData.data.expressSpec === expressTemplateItem.expressSpec
      && theData.data.incrementSpec === expressTemplateItem.incrementSpec
      && theData.data.incrementFee === expressTemplateItem.incrementFee
      && theData.data.freeMaxPayment === expressTemplateItem.freeMaxPayment
      && theData.data.freeCondition === expressTemplateItem.freeCondition) {
        // 数据一致，则是同一种运费模式
        theData.area.push({
          id: expressTemplateItem.id,
          province: expressTemplateItem.province
        })
        added = true
        break
      }
    }
    if (!added) {
      // 数据不一致，则是新的运费模式
      expressDataJson.push(buildData(expressTemplateItem))
    }
}

export const buildExpressData = (item) => {
  let data = buildData(item)
  let expressData = {
    expressCompany: item.expressCompany,
    expressData: [
      data
    ]
  }
  return expressData
}

export const buildData = (item) => {
  return {
    area: [
      {
        id: item.id,
        province: item.province
      }
    ],
    data: {
      expressFree: item.expressFree,
      expressFee: item.expressFee,
      expressSpec: item.expressSpec,
      incrementSpec: item.incrementSpec,
      incrementFee: item.incrementFee,
      freeMaxPayment: item.freeMaxPayment,
      freeCondition: item.freeCondition
    }
  }
}